# coding: utf-8
#FTP操作

import ftplib
import socket

def connect_ftp(host):
    #连接FTP
    timeout=5 # 超时时间，单位是s
    socket.setdefaulttimeout(timeout) 
    ftp = ftplib.FTP(host)
    return ftp

def login_ftp(ftp, user):
    #登录FTP
    ftp.login(user, user)

def ftp_download(ftp, filename):
    #以二进制形式下载文件
    bufsize = 1024  # 设置缓冲器大小
    fp = open(filename, 'wb')
    ftp.retrbinary('RETR %s' % filename, fp.write, bufsize)
    fp.close()

def ftp_upload(ftp, filename):
    #以二进制形式上传文件
    bufsize = 1024  # 设置缓冲器大小
    fp = open(filename, 'rb')
    ftp.storbinary('STOR ' + filename, fp, bufsize)
    fp.close()

def quit_ftp(ftp):
    ftp.quit()